
import numpy as np
from scipy.special import comb
from math import pow
from decimal import Decimal, getcontext
import matplotlib.pyplot as plt

def bernstein(n, original_function):
    def inner_function(x):
        sum = Decimal(0)
        for k in range(n + 1):
            one = comb(n, k)
            two = pow(float(x), k)
            three = pow(1 - float(x), n - k)
            four = float(original_function(Decimal(k) / Decimal(n)))
            sum += Decimal(one * two * three * four)
        return sum
    return inner_function

# 示例使用

if __name__ == '__main__':
    # 定义一个x的范围
    # 定义函数y = sin(x)
    xarray = np.linspace(0, 1, 256, endpoint=True)

    sinarray = [3258.86, 3301.93, 3217.74, 3284.32, 3201.29]
    yarray = [bernstein(2, lambda param: sinarray[int(param*4)])(x) for x in xarray]
    # 使用matplotlib来绘制函数图像
    plt.figure(figsize=(6, 4))  # 设置图像大小
    plt.plot(xarray, yarray, 'r')  # 绘制函数图像
    plt.plot(xarray, sinarray, 'b')
    plt.xlabel("x")  # 设置x轴标签
    plt.ylabel("y")  # 设置y轴标签
    plt.title("SSE Index")  # 设置标题
    plt.grid(True)  # 显示网格线
    plt.show()  # 显示图像
